package com.higgins.lawapp.server;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

public class GoogleDocumentService {
    String USERNAME = "pshigginswork@gmail.com";
    String PASSWORD = "Pa$$w0rd23";
    DocsService service = null;
    String documentListFeedUrl = "https://docs.google.com/feeds/default/private/full";
    
    public DocsService openService() throws AuthenticationException	{
	    service = new DocsService("LawFirmSheet");
	    service.setUserCredentials(USERNAME, PASSWORD);
		return service;
    }
    public List<String> retrieveCoveringLetters() throws IOException, ServiceException {
    	List<String> coveringLetters = new ArrayList<String>();
    	DocumentListFeed feed = service.getFeed(new URL(documentListFeedUrl),
    			DocumentListFeed.class);

    	for (DocumentListEntry entry : feed.getEntries()) {
    		addToDocumentList(entry, coveringLetters);
    	}
    	return coveringLetters;
	}

	public void addToDocumentList(DocumentListEntry doc, List<String> coveringLetters) {
		if (doc.getTitle().getPlainText().contains("Covering Letter Template"))
			coveringLetters.add(doc.getTitle().getPlainText());
	}    
}
